package com.wanglei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanglei.constants.SystemConstants;
import com.wanglei.domain.entity.Article;
import com.wanglei.domain.ResponseResult;
import com.wanglei.domain.entity.Category;
import com.wanglei.domain.vo.ArticleDetailVo;
import com.wanglei.domain.vo.ArticleListVo;
import com.wanglei.domain.vo.HotArticleVo;
import com.wanglei.domain.vo.PageVo;
import com.wanglei.mapper.CategoryMapper;
import com.wanglei.service.ArticleService;
import com.wanglei.mapper.ArticleMapper;
import com.wanglei.utils.BeanCopyUtils;
import com.wanglei.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author MrWang
* @description 针对表【sg_article(文章表)】的数据库操作Service实现
* @createDate 2023-06-29 16:45:03
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
    implements ArticleService{

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisCache redisCache;

    //查询热门文章列表
    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章 封装成ResponseResult返回
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL) //必须是正式文章
                .orderByDesc(Article::getViewCount); //按照浏览量进行排序
        //最多值查询10条
        Page<Article> page = new Page<>(1,10);
        articleMapper.selectPage(page, queryWrapper);

        List<Article> articles = page.getRecords();
        //bean拷贝
        //stream流方式(推荐)
        List<HotArticleVo> articleVos = BeanCopyUtils.copyBeanList(articles,HotArticleVo.class);

        return ResponseResult.okResult(articleVos);
    }

    //文章列表(可携带categoryId)
    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<Article>()
                //如果有categoryId 查询时要和传入的相同
                .eq(Objects.nonNull(categoryId)&&categoryId>0,Article::getCategoryId,categoryId)
                .eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL) //正式文章
                .orderByDesc(Article::getIsTop);//置顶 排序即可 1为置顶 0为不置顶

        //分页查询
        Page<Article> page = new Page<>(pageNum,pageSize);// 1 10
        articleMapper.selectPage(page,queryWrapper);
        List<Article> articles = page.getRecords();

        //给Article添加一个categoryName属性 并根据categoryId查询categoryName的值赋给它
//        for (Article article : articles) {
//            Category category = categoryMapper.selectById(article.getCategoryId());
//            article.setCategoryName(category.getName());
//        }
        //stream流写法（推荐！！！）
        articles.stream().map(article -> {
            article.setCategoryName(categoryMapper.selectById(article.getCategoryId()).getName());
            return article;
        }).collect(Collectors.toList());

        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(articles, ArticleListVo.class);
        PageVo pageVo = new PageVo((List) articleListVos,page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    //查询文章详情
    @Override
    public ResponseResult getArticleDetail(Long id) {
        //根据id查询文章
        Article article = articleMapper.selectById(id);
        //4. 读取文章浏览量时从redis读取 getCacheMapValue(k,hk) 获取k为k的map种键位hk的值
        Integer viewCount = redisCache.getCacheMapValue("article:viewCount", id.toString());
        article.setViewCount(viewCount.longValue());
        //vo
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        //根据分类id查询分类名
        Category category = categoryMapper.selectById(articleDetailVo.getCategoryId());
        if (category != null){//防止空指针异常
            articleDetailVo.setCategoryName(category.getName());
        }

        //封装响应返回
        return ResponseResult.okResult(articleDetailVo);
    }

    //2. 更新文章浏览量到redis
    @Override
    public ResponseResult updateViewCount(Long id) {
        redisCache.incrementCacheMapValue("article:viewCount",id.toString(),1);//浏览量+1
        return ResponseResult.okResult();
    }


}




